package org.example.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import jakarta.annotation.Resource;
import org.example.mapper.ElderlyAnalyzeMapper;
import org.example.pojo.Elderly;
import org.example.pojo.ListMap;
import org.example.service.ElderlyAnalyzeService;
import org.example.utils.PageBean;
import org.example.utils.R;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * @Description:
 * @Author:曹震
 * @Date: 2024/12/9 9:40
 */
@Service
public class ElderlyDataAnalyzeServiceImpl implements ElderlyAnalyzeService {
    @Resource
    private ElderlyAnalyzeMapper mapper;
    @Override
    public R sortEdler(int pageSize, int currentPage) {
        PageHelper.startPage(pageSize, currentPage);
        List<Elderly> elderly = mapper.sortListElderly();

        PageInfo<Elderly> elderlyPageInfo = new PageInfo<>(elderly);
        PageBean<Elderly> elderlyPageBean =
                new PageBean<>(pageSize, currentPage, elderlyPageInfo.getTotal(), elderlyPageInfo.getList());
        return R.successful(null, elderlyPageBean);
        }

    @Override
    public Elderly listElderlyTask(String elderlyId) {
        Elderly elderly = mapper.listElderlyTask(elderlyId);
        return elderly;
    }

    @Override
    public String listElderlyMap(String elderlyId) {
        //折线图:统计当月发送请求的次数
//        List<ListMap> monthlyTaskCounts = mapper.getMonthlyTaskCounts(elderlyId);
//        ArrayList<String> months = new ArrayList<>();
//        monthlyTaskCounts.forEach(item -> months.add(item.getName()));
//        ArrayList<Integer> count = new ArrayList<>();
//        monthlyTaskCounts.forEach(item->count.add(item.getCount()));
        // 从数据库查询结果
        List<ListMap> queryResults = mapper.getMonthlyTaskCounts(elderlyId);

        // 初始化结果，1到12月每月初始化为0
        Map<Integer, Integer> completeResults = new TreeMap<>();
        for (int month = 1; month <= 12; month++) {
            completeResults.put(month, 0);
        }
        // 填充实际数据
        for (ListMap result : queryResults) {
            int month =(int) result.getName();
            int count = result.getCount();
            completeResults.put(month, count);
        }
        Gson gson = new Gson();
        String json = gson.toJson(completeResults);
        return  json;
        //没有设置表名，交给前端
    }
}

